import axios from 'axios'

const instance = axios.create({
    baseURL: '/api/',
    timeout: 10000,
    headers: {'X-Custom-Header': 'foobar'}
  });

// 添加请求拦截器
instance.interceptors.request.use(function (config) {
    const token=localStorage.getItem('token')
    if(token){
        config.headers.Authorization=`Bearer ${token}`
    }
    // 在发送请求之前做些什么
    return config;
  }, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
  });

// 添加响应拦截器
instance.interceptors.response.use(function (response) {
    // 对响应数据做点什么
    return response;
  }, function (error) {
    // 对响应错误做点什么
    if (error.response) {
        const status=error.response.status
        switch(status){
            case 400:
                console.log('未授权');
            break
            case 500:
                console.log('浏览器错误');
            break
                
                
        }
      } else if (error.request) {
        // The request was made but no response was received
        // `error.request` is an instance of XMLHttpRequest in the browser and an instance of
        // http.ClientRequest in node.js
        console.log(error.request);
      } else {
        // Something happened in setting up the request that triggered an Error
        console.log('Error', error.message);
      }
    return Promise.reject(error);
  });
export default instance




